package edu.cth.speedapp.student;

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

import edu.cth.speedapp.DeviceUuidFactory;
import edu.cth.speedapp.R;
import edu.cth.speedapp.asynctasks.HTTPAsyncTask;
import edu.cth.speedapp.asynctasks.ITaskCallback;
import android.R.fraction;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class StudentActivity extends Activity implements ITaskCallback {
	private String deviceUuid;
	
	private Timer timer;
	Fragment oldFragment;
	String currentMode = "none";
	String roomName;

	
	//Fragment helpFragment, tempoFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_container);
		

        Bundle extras = getIntent().getExtras();
        
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        deviceUuid = deviceUuidFactory.getDeviceUuid().toString();
        
    	
		
		String mode = extras.getString("mode");    		
		roomName = extras.getString("roomName");    		
		
		changeMode(mode);
		
	}
	
	TimerTask updateRoomModeTask = new TimerTask(){
		@Override
		public void run() {
			updateRoomMode();
		}

		
	};

	
	private void updateRoomMode() {
		//Toast.makeText(this, "STUDENTACTIVITY SEND", Toast.LENGTH_SHORT).show();
		new HTTPAsyncTask(this).execute(getString(R.string.baseURL) + "/getRoomMode.php?roomID=" + roomName);
		
	}
	
	private void changeMode(String mode){
		
		if(!currentMode.equals(mode)){
			Toast.makeText(this, "Change mode: " + mode, Toast.LENGTH_SHORT).show();
			currentMode = mode;
			
			FrameLayout fl = (FrameLayout) this.findViewById(R.id.fragment_container);
			fl.removeAllViews();
			if(mode.equals("tempo")){
				getFragmentManager().beginTransaction().replace(R.id.fragment_container, new StudentTempo(roomName, deviceUuid)).commit();
			}else if (mode.equals("helpqueue")){
				getFragmentManager().beginTransaction().replace(R.id.fragment_container, new StudentHelp(roomName, deviceUuid)).commit();
			}



			
		}
		
	}
	
	 @Override
	public void onDestroy() {
    	Toast.makeText(this, "Leaving room...", Toast.LENGTH_SHORT).show();
    	HTTPAsyncTask t = new HTTPAsyncTask(new ITaskCallback() {
			
			public void httpTaskCallback(String results) {
				return;
			}
		});
    	t.execute(getString(R.string.baseURL) + "/leaveRoom.php?roomName=" + roomName + "&studentID=" + deviceUuid);
    	super.onDestroy();
    }
	
	@Override
	protected void onResume() {
		timer = new Timer();
		try{
			timer.schedule(updateRoomModeTask, 0, 5000);
		}
		catch(IllegalStateException e){
			
			
		}
		super.onResume();
	}
	@Override
	protected void onPause() {
		super.onPause();
		timer.cancel();
        timer.purge();
        timer = null;
	}

	public void httpTaskCallback(String results) {
		
		Scanner scan = new Scanner(results);
		scan.next();
		String mode = scan.next();
		changeMode(mode);
		
	}


}
